define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Myfriendjxs,GoDialog,$timeout,GoPage) {
		
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data();
		}, false);
		
		//加载数据
		$scope.init_data = function(){
			Myfriendjxs.get_friend_list(1,function(data){
//				console.log(data)
				$scope.friends = data;
			})
		}
		
		//跳转到店铺首页
		$scope.onshow_first = function($event,store_id){
			if(!store_id) return;
			GoPage.go_states("first",{"store_id":store_id});
		}
		
		//删除
		$scope.deleteStore = function(id,index){
			Myfriendjxs.deleteStore(id,function(data){
				if(data.success) {
					GoDialog.toast("删除成功！");
					$scope.friends.store.splice(index,1);//删除一条数据
				}else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		$scope.switchEvent = function(buttonId,store_id,index){
			switch(buttonId) {
				case 1:
				{ //删除
					GoDialog.confirm("提示","确定要删除吗？",function(){
						$scope.deleteStore(store_id,index);
					})
					break;
				};
				case 2:
				{ //还款
					GoPage.go_states("credit_repayment",{"id":store_id});		
					break;
				};
			}		
		}
		
		/*--------------上下拉刷新-----------------*/
		$scope.nPage = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function(){
			$scope.isLoad = true;
			Myfriendjxs.get_friend_list(1,function(data){
				$scope.friends = data;
				$scope.nPage = 1;
			},function(){
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			Myfriendjxs.get_friend_list($scope.nPage+1,function(data){
				if(data!=null){
					if($scope.friends.store!=undefined){
						$scope.friends.store = $scope.friends.store.concat(data.store);
						$scope.nPage += 1;
					}else{
						$scope.friends = data;
					}
				}
			},function(){
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		
		
    }

    controller.$inject = ['$scope', 'Myfriendjxs','GoDialog','$timeout','GoPage'];

    return app.registerController("myfriendjxsctrl",controller);
});